﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoodsInvoicing.BLL.Service;
using GoodsInvoicing.BLL.ServiceImpl;
using GoodsInvoicing.DAL.DAO.UserRole;
using GoodsInvoicing.Model.User;
using GoodsInvoicing.Model.UserRole;
using GoodsInvoicing.Views.View;
using Spring.Context;
using Spring.Context.Support;
using GoodsInvoicing.Presentions.Presenter;
using GoodsInvoicing.Presentions.PresenterImpl;
using GoodsInvoicing.Model.Menu;
using System.Collections;

namespace GoodsInvoicing
{
    public partial class MainForm : Form, IMainForm
    {
        static log4net.ILog logger = log4net.LogManager.GetLogger("Logger");
        private IApplicationContext applicationContext;

        private IMainFormPresenter presenter;
        private UserDetail user = null;//存储登录用户的全部信息
        private MainForm mainForm = null;//存储主菜单的指针

        public MainForm()
        {
            InitializeComponent();

            //log4net.Config.XmlConfigurator.Configure();
            applicationContext = ContextRegistry.GetContext();
            presenter = new MainFormPresenter(this, applicationContext["GI_MenuService"] as IGI_MenuService);
            presenter.InitializeView();
        }

        #region IMainForm 成员
        public event EventHandler<EventArgs> MainMenuLoad;
        public event EventHandler SubMenuClick;

        public UserDetail User 
        { 
            get
            {
                return user;
            } 
            set
            {
                this.user = value;
            } 
        }

        //主菜单
        public MainForm Form 
        {
            get
            {
                return mainForm;
            }
            set 
            {
                this.mainForm = value;
            }
        }
        #endregion



        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //IApplicationContext applicationContext = ContextRegistry.GetContext();

                //IApplicationContext context =  ContextRegistry.GetContext();

                //GI_UserDAO<GoodsInvoicing.Model.UserRole.GI_User> dao = (GI_UserDAO<GoodsInvoicing.Model.UserRole.GI_User>)applicationContext.GetObject("GI_UserDAO");
                //GI_UserDAO dao = (GI_UserDAO)applicationContext.GetObject("GI_UserDAO");
                //GI_User newUser = (GI_User)dao.GetGI_User(1);

               // GI_UserDAO dao = (GI_UserDAO)applicationContext.GetObject("GI_UserDAO");
                //GI_User user = (GI_User)dao.Get(1);
                
                //IList<GI_User> userList = dao.GetAll();
                //foreach (var GI_User in userList)
                //{
                    //MessageBox.Show(GI_User.UserID + " " + GI_User.UserName);
                //}

               // GI_RoleDAO roleDao = (GI_RoleDAO)applicationContext.GetObject("GI_RoleDAO");
                //GI_Role role = (GI_Role)roleDao.Get(1);

                IGI_UserService userService = (IGI_UserService)applicationContext.GetObject("GI_UserService");
                GI_User newUser = userService.Get(1);

                //GI_UserService mgr = applicationContext["GI_UserService"] as GI_UserService;
                //GI_User newUser = mgr.Get(1);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                logger.Error(ex);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GI_PermissionDAO dao = (GI_PermissionDAO)applicationContext.GetObject("GI_PermissionDAO");
            GI_Permission permission = (GI_Permission)dao.Get(1);

            MessageBox.Show(permission.PermissionID + "-" + permission.PermissionName);

            GI_Permission newPermission = new GI_Permission();
            newPermission.PermissionName = "wrong num";
            dao.Save(newPermission);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                IGI_PermissionService service = (IGI_PermissionService)applicationContext.GetObject("GI_PermissionService");
                GI_Permission permission = service.Get(1);
                MessageBox.Show(permission.PermissionID + " " + permission.PermissionName );
                
                string value = typeof(GI_Permission).Name;

            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (this.MainMenuLoad != null)
            {
                this.MainMenuLoad(sender, e);
            }
        }

        /// <summary>
        /// 生成菜单
        /// </summary>
        /// <param name="menus"></param>
        public void ShowMenu(IDictionary<GI_Menu, IList<GI_Menu>> menus)
        {
            //生成一级菜单
            foreach (KeyValuePair<GI_Menu, IList<GI_Menu>> kvp in menus)
            {
                var firstLevelMenu = new ToolStripMenuItem() { Text = kvp.Key.MenuName, Name = kvp.Key.MenuID.ToString() };//需要增加
                SetParentMenu(firstLevelMenu);
                var secondLevelMenu = kvp.Value;//对应的二级菜单

                //遍历二级菜单
                foreach (var menu in secondLevelMenu)
                {
                    var secondMenu = new ToolStripMenuItem() { Text = menu.MenuName, Name = menu.MenuID.ToString() };//需要增加
                    SetChildMenu(secondMenu);
                    firstLevelMenu.DropDownItems.Add(secondMenu);
                }

                this.menuMain.Items.Add(firstLevelMenu);
            }
        }

        /// <summary>
        /// 设置二级菜单处理事件
        /// </summary>
        /// <param name="secondMenu"></param>
        private void SetChildMenu(ToolStripMenuItem secondMenu)
        {
            secondMenu.Click += SubMenuClick;
        }

        /// <summary>
        /// 设置一级菜单处理事件
        /// </summary>
        /// <param name="firstLevelMenu"></param>
        private void SetParentMenu(ToolStripMenuItem firstLevelMenu)
        {
            //一级菜单事件暂无
        }
    }
}
